using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAL
{
    public class CauHoiBinhLuan_DAL
    {
        private SqlConnection cn = null;
        public  CauHoiBinhLuan_DAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
        public void Insert(CauHoiBinhLuan_DTO cauHoiBinhLuan)
        {
            SqlCommand cm = new SqlCommand("tb_CauHoiBinhLuan_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            //cm.Parameters.AddWithValue("@ID", cauHoiBinhLuan.ID);
            cm.Parameters.AddWithValue("@MaMH", cauHoiBinhLuan.MaMH);
            cm.Parameters.AddWithValue("@MaKH", cauHoiBinhLuan.MaKH);
            cm.Parameters.AddWithValue("@Ngay", cauHoiBinhLuan.Ngay);
            cm.Parameters.AddWithValue("@NoiDung", cauHoiBinhLuan.NoiDung);
            cm.ExecuteNonQuery();
        }
        public void Update(CauHoiBinhLuan_DTO cauHoiBinhLuan)
        {
            SqlCommand cm = new SqlCommand("tb_CauHoiBinhLuan_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@ID", cauHoiBinhLuan.ID);
            cm.Parameters.AddWithValue("@MaMH", cauHoiBinhLuan.MaMH);
            cm.Parameters.AddWithValue("@MaKH", cauHoiBinhLuan.MaKH);
            cm.Parameters.AddWithValue("@Ngay", cauHoiBinhLuan.Ngay);
            cm.Parameters.AddWithValue("@NoiDung", cauHoiBinhLuan.NoiDung);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("tb_CauHoiBinhLuan_Delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@ID", iD);
            cm.ExecuteNonQuery();
        }
        public tbCauHoiBinhLuanCollection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("tb_CauHoi_BinhLuan_Get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbCauHoiBinhLuanCollection cauHoiBinhLuanCollection = new tbCauHoiBinhLuanCollection();

            while (dr.Read())
            {
                CauHoiBinhLuan_DTO cauHoiBinhLuan = new CauHoiBinhLuan_DTO();
                cauHoiBinhLuan.ID = int.Parse(dr.GetValue(0).ToString());
                cauHoiBinhLuan.MaKH = int.Parse(dr.GetValue(1).ToString());
                cauHoiBinhLuan.MaMH = int.Parse(dr.GetValue(2).ToString());
                cauHoiBinhLuan.Ngay = DateTime.Parse(dr.GetValue(3).ToString());
                cauHoiBinhLuan.NoiDung = dr.GetValue(4).ToString();
                cauHoiBinhLuanCollection.Add(cauHoiBinhLuan);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return cauHoiBinhLuanCollection;
        }
    }
}
